//
//  KLTodayGoodViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLCarSaleViewController.h"
#import "KLCarSaleTableViewCell.h"
#import "KLHotelDetailViewController.h"
@interface KLCarSaleViewController ()
@property(nonatomic, strong) STNoresultView                     *noResultView;
@property(nonatomic, strong) NSMutableArray <KLGoodModel*>                    *dataSouce;
@end

@implementation KLCarSaleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"民车出租";
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendEspeciallyGoodListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.tableHeaderView = [[KLSearchTableHeader alloc] initWithHandle:^{
        
        KLGlobalSearchViewController * vc = [KLGlobalSearchViewController new];
        NSString * num = [[KLClassify defult] numFormTitle:self.title];
        vc.type = num.integerValue;
        vc.currentVC = self;
        STNavigationController * nav = [[STNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
        
    }];
//    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 100, UIScreenWidth, 350)
//                                                        title:@"暂无出租"
//                                                  buttonTitle:@""
//                                                 buttonHandle:^(NSString *titleString) {
//                                                     
//                                                 }];
//    self.noResultView.hidden = YES;
//    [self.tableView addSubview:self.noResultView];
    // Do any additional setup after loading the view.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLCarSaleTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLCarSaleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLCarSaleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.goodModel = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KLGoodModel * model = self.dataSouce[indexPath.row];
    KLHotelDetailViewController * vc = [KLHotelDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)sendEspeciallyGoodListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodslist"];
    NSString * num = [[KLClassify defult] numFormTitle:self.title];
    [dic setObject:num forKey:@"classification"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * list = dataDic[@"list"];
                                                     for (NSDictionary * listdic in list) {
                                                         KLGoodModel * model = [KLGoodModel  mj_objectWithKeyValues:listdic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             }];
}

@end


